package dao

import (
	"advertising/internal/dal"
	"advertising/internal/models"
	"context"
	"strings"
	"time"

	"192.168.1.75/go-pkg/helper"
	"gorm.io/gorm"
)

type PublishScheduleMaterial struct{}

func (PublishScheduleMaterial) Create(ctx context.Context, db *gorm.DB, merchantID, ScheduleID, MaterialID string, Duration int64, PlayType int8, RegularInterval int64) (err error) {
	m := dal.Use(db).PublishScheduleMaterial
	tx := m.WithContext(ctx)
	var d models.PublishScheduleMaterial
	d.ID, _ = helper.UUID{}.String()
	d.MerchantID = merchantID
	d.ScheduleID = ScheduleID
	d.MaterialID = MaterialID
	d.Duration = Duration
	d.PlayType = PlayType
	d.RegularInterval = RegularInterval
	d.CreatedAt = time.Now().Unix()
	tx.Create(&d)
	return
}

// 更新投放计划素材的时长
func (PublishScheduleMaterial) UpdateDuration(ctx context.Context, db *gorm.DB, scheduleID, materialID string, duration int64, playType int8, regularInterval int64) {
	m := dal.Use(db).PublishScheduleMaterial
	m.WithContext(ctx).
		Where(m.ScheduleID.Eq(scheduleID)).
		Where(m.MaterialID.Eq(materialID)).
		UpdateSimple(
			m.Duration.Value(duration),
			m.PlayType.Value(playType),
			m.RegularInterval.Value(regularInterval),
		)
}

// 投放计划素材列表
func (PublishScheduleMaterial) List(ctx context.Context, db *gorm.DB, scheduleID, materialID string, page, limit int) (res []*models.PublishScheduleMaterial, count int64, err error) {
	m := dal.Use(db).PublishScheduleMaterial
	tx := m.WithContext(ctx).
		Where(m.DeletedAt.Eq(0))
	if scheduleID != "" {
		if strings.Contains(scheduleID, ",") {
			tx = tx.Where(m.ScheduleID.In(strings.Split(scheduleID, ",")...))
		} else {
			tx = tx.Where(m.ScheduleID.Eq(scheduleID))
		}
	}
	if materialID != "" {
		if strings.Contains(materialID, ",") {
			tx = tx.Where(m.MaterialID.In(strings.Split(materialID, ",")...))
		} else {
			tx = tx.Where(m.MaterialID.Eq(materialID))
		}
	}
	return tx.FindByPage(Offset(page, limit))
}

type PublishScheduleMaterialList struct {
	models.PublishScheduleMaterial
	// 素材属性
	Name     string `json:"name" gorm:"column:name"`
	FilePath string `json:"file_path" gorm:"column:file_path"`
	CheckMd5 string `json:"check_md5" gorm:"column:check_md5"`
	Type     int8   `json:"type" gorm:"column:type"`
	FileSize int64  `json:"file_size" gorm:"column:file_size"`
	Position int8   `json:"position" gorm:"column:position"`
}

// 投放计划素材列表附带素材信息
func (PublishScheduleMaterial) MaterialList(ctx context.Context, db *gorm.DB, scheduleID string) (res []PublishScheduleMaterialList, err error) {
	sm := dal.Use(db).PublishScheduleMaterial
	m := dal.Use(db).Material
	tx := sm.WithContext(ctx)
	tx.Join(m, m.ID.EqCol(sm.MaterialID)).
		Where(sm.ScheduleID.In(strings.Split(scheduleID, ",")...)).
		Where(sm.DeletedAt.Eq(0)).
		Where(m.DeletedAt.Eq(0)).
		Select(sm.ALL, m.Name, m.FilePath, m.FileSize, m.CheckMd5, m.Type, m.Position).
		Order(sm.CreatedAt).
		Scan(&res)
	return
}

// 投放计划的素材排序
func (PublishScheduleMaterial) Sort(ctx context.Context, db *gorm.DB, ID string, sort int8) (err error) {
	m := dal.Use(db).PublishScheduleMaterial
	m.WithContext(ctx).
		Where(m.ID.Eq(ID)).
		UpdateColumn(m.Sort, sort)
	return nil
}

func (PublishScheduleMaterial) Delete(ctx context.Context, db *gorm.DB, scheduleID, materialID string) (err error) {
	m := dal.Use(db).PublishScheduleMaterial
	m.WithContext(ctx).
		Where(m.ScheduleID.Eq(scheduleID)).
		Where(m.MaterialID.In(strings.Split(materialID, ",")...)).
		UpdateColumn(m.DeletedAt, time.Now().Unix())
	return nil
}
